** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_C1.smcl", replace

use "$project_path/data/4_regdata/regdata", clear

foreach v in diff_m1_chix_econ std_diff_m1_growth_gdpc_penn mstd_diff_m1_inflation_imf mstd_diff_m1_unemployment_ilo std_diff_m1_trade_wb std_diff_m1_hdi std_diff_m1_democracy_index diff_m1_general_chix {
	gen m_`v' = (`v'==.)
}

rdrobust m_diff_m1_chix_econ runvar, all
rdrobust m_std_diff_m1_growth_gdpc_penn runvar, all
rdrobust m_mstd_diff_m1_inflation_imf runvar, all
rdrobust m_mstd_diff_m1_unemployment_ilo runvar, all
rdrobust m_std_diff_m1_trade_wb runvar, all
rdrobust m_std_diff_m1_hdi runvar, all
rdrobust m_std_diff_m1_democracy_index runvar, all
rdrobust m_diff_m1_general_chix runvar, all

log close


** Building .tex table
local varlist m_diff_m1_chix_econ 			 	///
			  m_std_diff_m1_growth_gdpc_penn 	///
			  m_mstd_diff_m1_inflation_imf	 	///
			  m_mstd_diff_m1_unemployment_ilo 	///
			  m_std_diff_m1_trade_wb 			///
			  m_std_diff_m1_hdi 				///
			  m_std_diff_m1_democracy_index 	///
			  m_diff_m1_general_chix

local rows : word count `varlist'
//Prepare matrix of coefficients and other statistics
local nrows `rows'
local ncols 4
mat Coefs = J(`nrows', `ncols', .)

local i = 1			  
foreach var in `varlist'{

	rdrobust `var' runvar, all
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	loca N_`i': di %6.0f e(N)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''	
	mat Coefs[`i',4] = `N_`i''
	
	local ++i
	
}			  

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 estimates
rename Coefs2 std_err
rename Coefs3 pval
rename Coefs4 N

label var estimates "Est."
label var std_err "SE"
label var pval "p-val"
label var N "N"

gen outcome = ""
replace outcome = "\textbf{Economic performance}" in 1
replace outcome = "GDP per capita growth" in 2
replace outcome = "Inflation" in 3
replace outcome = "Unemployment" in 4
replace outcome = "Trade intensity" in 5
replace outcome = "\textbf{Human Development Index}" in 6
replace outcome = "\textbf{Democracy}" in 7
replace outcome = "\textbf{General index}" in 8

label var outcome "Missing outcome"

order outcome, first

tostring estimates, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace estimates = estimates + "***" if pval < `3_star'
replace estimates = estimates + "**" if  pval < `2_star' & pval > `3_star'
replace estimates = estimates + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}

replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"


local title "Missing outcome data"
local label_key "tab:robustness_missing"
local output "$project_path/output/appendix_tables/Appendix_Table_C1.tex"
local footnote = "\caption*{\footnotesize \emph{Notes}: This table presents estimation results for equation (\ref{eq:baseline_rdd}) using as a dependent variable a dummy equal to 1 if the outcome has missing data.}"

texsave using "`output'", replace varlabels frag nofix 	location(h) ///
						  align(p{10cm} p{1.7cm} p{1.7cm} p{1.7cm} p{1.7cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'")
			





	




